//
//  loginPhoneViewController.m
//  QuickCare
//
//  Created by 孙宏毅 on 16/7/11.
//  Copyright © 2016年 lidabiao. All rights reserved.
//

#import "loginPhoneViewController.h"
#import "findPasswordController.h"
#import "registerNewAccountController.h"
#import "CoreDataManager.h"
#import "SSKeychain.h"
#import "UserInfo+CoreDataProperties.h"
#import "DrawerManager.h"

@interface loginPhoneViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneNumTextField;

@property (weak, nonatomic) IBOutlet UITextField *passWordTextField;


@end

@implementation loginPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

//点击登录
- (IBAction)loginButtonClick:(id)sender
{
    if (self.phoneNumTextField.text.length == 0)
    {
        NSLog(@"请填写帐号");
        
        return;
    }
    
    if (self.passWordTextField.text.length == 0)
    {
        NSLog(@"请填写密码");

        return;
    }
    
    NSArray *arr = [kCoreDataManager executeFetchWithEntityName:@"UserInfo" format:[NSString stringWithFormat:@"phoneNum == %@", self.phoneNumTextField.text]];
    
    if (arr == nil || arr.count == 0)
    {
        NSLog(@"该用户不存在");
        
        return;
    }
    else
    {
        UserInfo *userInfo = arr.firstObject;
        
        NSString *bundleId = [NSBundle mainBundle].bundleIdentifier;
        
        NSString *passWord = [SSKeychain passwordForService:bundleId account:userInfo.phoneNum];
        
        if ([self.passWordTextField.text isEqualToString:passWord])
        {
            NSLog(@"登录成功");
            
            userInfo.isLogin = @1;
            
            [kCoreDataManager save];
            
            [kDrawerManager enableLeftDrawerAndOpenLeftDrawer];
        }
        else
        {
            NSLog(@"密码错误");
        }
    }
    
}

//找回密码
- (IBAction)clickFindPassword:(id)sender {
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"findPasswordController" bundle:nil];
    
    findPasswordController *findVc = [sb instantiateInitialViewController];
    
    [self.navigationController pushViewController:findVc animated:YES];
    
}

//点击注册
- (IBAction)registerNewAccountButton:(id)sender {
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"registerNewAccountController" bundle:nil];
    
    registerNewAccountController *newVc = [sb instantiateInitialViewController];
    
    [self.navigationController pushViewController:newVc animated:YES];
    
}

//点击短信登录
- (IBAction)loginMessage:(id)sender {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"loginMessageViewController" bundle:nil];
    
    findPasswordController *loginMessage = [sb instantiateInitialViewController];
    
    [self.navigationController pushViewController:loginMessage animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
